
package com.feib.ebilling.job.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.feib.ebilling.job.dao.BillFeedbackDao;
import com.feib.ebilling.job.util.StringUtils;


@Service("billFeedbackAutoloansService")
public class BillFeedbackAutoloansService extends BaseService  {
    
	 public static Map<String, String> CHANEL_CODE = new HashMap<String, String>();
	    static
	    {
	        CHANEL_CODE.put("7111111", "1");
	        CHANEL_CODE.put("TFM", "2");
	        CHANEL_CODE.put("HILIFE", "3");
	        CHANEL_CODE.put("OKCVS", "4");
	        CHANEL_CODE.put("POST", "5");
	    }
    @Resource(name="billFeedbackDao")
    private BillFeedbackDao billFeedbackDao;     
   
    /**
     * @return
     */
    public List<Map<String, Object>> loadGroupPK(String GroupNo) {
        return this.billFeedbackDao.queryGroupPKByGroupNo(GroupNo);
    }
    
    /**
     * @return
     */
    public List<Map<String, Object>> loadGroups() {
        return this.billFeedbackDao.queryALLGroup();
    }
    
    @SuppressWarnings("finally")
	public String writeContent(String GroupType, String processDate)
    {
    	StringBuffer sbf = new StringBuffer();    	
    	try{	    	    
	       	/* 車貸策略聯盟超商、郵局代收回饋明細 */
	    	List<Map<String, Object>> payments = this.billFeedbackDao.queryFeedbacklogByThirdPartyAndGroupType(GroupType, processDate);
	    	long total = 0;
	    	long amount = 0;     
	    	for(Map<String, Object> payment : payments)
	        {
	    		/* 銷帳帳號 */
	    		sbf.append(StringUtils.PadString("L", " ", 16, (String)payment.get("USERNO")));
	    		/* 入/扣帳日期 */
	    		sbf.append(StringUtils.PadString("L", " ", 8, processDate));
	    		/* 繳費日期 */
	    		sbf.append(StringUtils.PadString("L", " ", 8, (String)payment.get("ADAY")));
	    		/* 繳費金額 */
	    		BigDecimal bd = (BigDecimal) payment.get("TXAMT");
	    		/* 9(10)V99 */
	    		sbf.append(StringUtils.PadString("R", "0", 10, bd.toString()));
	    		/* 通路別 */
	    		String channel =(String)payment.get("CHANNEL");	    		
	    		sbf.append(StringUtils.PadString("L", " ", 1, CHANEL_CODE.get(channel)));
	    		total++;
	    		amount = amount + Long.parseLong(bd.toString());
	    		sbf.append("\r\n");
	        }
	    	logger.info("車貸策略聯盟超商、郵局代收筆數：{}" , String.valueOf(total));
	    	logger.info("車貸策略聯盟超商、郵局代收金額：{}" , String.valueOf(amount));	    	
    	 }catch (Exception e) {
            logger.error("[錯誤]" + e.getMessage(), e);
         }
         finally{
            return sbf.toString();            
         }
    }
	
}
